# -*- coding: utf-8 -*-
# FileName:     movable_widget.py
# time:         22/11/25 025 下午 9:38
# Author:       Zhou Hang
# Description:  I don't want to write
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWidgets import QWidget


class MovableWidget(QWidget):
    """可以鼠标拖动的Widget类"""
    def __init__(self, parent=None):
        super(MovableWidget, self).__init__(parent)
        self._tracking = False
        self._startPos = None
        self._endPos = None
        # 设置 窗口无边框和背景透明 *必须
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(Qt.FramelessWindowHint)

    # 无边框情况下也能移动窗口
    def mouseMoveEvent(self, e: QMouseEvent):  # 重写移动事件
        if self._tracking:
            self._endPos = e.pos() - self._startPos
            self.move(self.pos() + self._endPos)

    def mousePressEvent(self, e: QMouseEvent):
        if e.button() == Qt.LeftButton:
            self._startPos = QPoint(e.x(), e.y())
            self._tracking = True

    def mouseReleaseEvent(self, e: QMouseEvent):
        if e.button() == Qt.LeftButton:
            self._tracking = False
            self._startPos = None
            self._endPos = None


def main():
    pass


if __name__ == "__main__":
    main()
